<?php
	defined('_JEXEC') or die('Restricted access');
   
	//
	// t_base_step
	//
	abstract class t_base_step
	{
		// property declaration
		// protected
		
        // property declaration
		// private
		 
		// property declaration
		// public
		// C
		public $code = '';
        const common_properties_position_per_step_type = 1;
        const common_properties_position_per_step_name = 2;
		const common_properties_position_per_field_name = 3;        
        const common_properties_position_last = 4;
        // F
		public $field = null;
        //N
        public $name = '';
        

        
		// property declaration
		// protected
        // C
        protected $common_properties_array = array();
        // P
        protected $proper_properties_array = array();
        // T
        

		// method declaration
		// private
        
        // method declaration
		// protected    
		//A
		
		// G
        abstract protected function get_proper_properties_position_last();
	


        // method declaration
		// public        
		public function __construct($a_common_properties_str, $a_proper_properties_str) 
		{
            global $g_cuteengine;
            global $g_strings_utility_namespace;
			//echo "a_common_properties_str: ".$a_common_properties_str. "<br>";
           /*  COMMON PROPERTIES  */
            $this->common_properties_array = $g_strings_utility_namespace->check_string_parameters(
				$this,"",t_query_parameter::separator_level_1,$a_common_properties_str,
				self::common_properties_position_last,'COMMON PROPERTIES');                    
            $this->name = $this->common_properties_array[self::common_properties_position_per_step_name];
    
            /*  PROPER PROPERTIES  */    
            $this->proper_properties_array = $g_strings_utility_namespace->check_string_parameters(
				$this,$this->name,t_query_parameter::separator_level_1,$a_proper_properties_str,
				$this->get_proper_properties_position_last(),'PROPER PROPERTIES');  
		}
        // A
       
		
		

        // C
        public static function create_step_from_string($a_step_str)
        {
            //echo "a_step_str: $a_step_str<br>";
            global $g_cuteengine;
            global $g_ambient_namespace;
			$local_step = null;
			$local_step_properties = explode(t_query_parameter::separator_level_1,$a_step_str);
            $local_common_properties_str = implode(t_query_parameter::separator_level_1, array_slice($local_step_properties,0,self::common_properties_position_last)) . 
                t_query_parameter::separator_level_1; 
            $local_proper_properties_str = t_query_parameter::separator_level_1 . 
                implode(t_query_parameter::separator_level_1, array_slice($local_step_properties, self::common_properties_position_last));
            //
			$local_class_type = $local_step_properties[self::common_properties_position_per_step_type];  
            $local_name = $local_step_properties[self::common_properties_position_per_step_name]; 			
			$local_file_name = substr($local_class_type,2,strlen($local_class_type));
            $local_path = CUTEENGINE_STEPS_PATH. $local_file_name . DIRECTORY_SEPARATOR  ;
           // echo "$local_path$local_file_name.php <br>";   
            if (file_exists("$local_path$local_file_name.php"))
            {      
     			require_once("$local_path$local_file_name.php"); 
    			//echo "local_class_type:" .$local_class_type."<br>"; 
                $g_ambient_namespace->autoload_content = 'steps_content';
                $local_step = new $local_class_type(
                    $local_common_properties_str,$local_proper_properties_str);	        
            } 
			else
			{
				 throw new Exception('Error step class '.$local_class_type.'. Class file not found.');
			} 
            
			return $local_step;
        }	
        // G
		public function get_field_name()
		{
			
			return $this->common_properties_array[self::common_properties_position_per_field_name];  
		} 
		public function set_field_name($a_field_name)
		{	
			$this->common_properties_array[self::common_properties_position_per_field_name] = $a_field_name;  
		}		
        // I
        public function initialize()
        {		
			global $g_cuteengine;
			//echo "initializing step ".$this->name."<br>";
			if (! $this->initialized)
			{
				$this->initialized = false;
				//
                $local_field_name = $this->get_field_name();  
				//echo "local_field_name: $local_field_name<br>";
                if ($local_field_name !== '')
                {
                    
                    $this->field = $g_cuteengine->fields_list[$local_field_name];   
                    //echo "step '$this->name' this->field->name: ".$this->field->name."<br>";
					//print_r($this);
                    if (! isset($this->field))
                    {
                       
                        throw new Exception('Error step class '.$this->name.". Field \"$local_field_name\" not found.");
                    }
                }    
            }    
        }  

	}
?>